﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using BL;
namespace QuanLyCHTrangSuc
{
    public partial class frmQLyDonHang : Form
    {
        List<DonDatHangDAL> dsDonHang = new List<DonDatHangDAL>();
        List<ChiTietDonDatHangDAL> dsChiTietDDH = new List<ChiTietDonDatHangDAL>();
        List<SanPhamDAL> dsSanPham = new List<SanPhamDAL>();
        DonDatHangDAL dh = new DonDatHangDAL();
        DonDatHangBL donHang = new DonDatHangBL();
        ChiTietDonDatHangBL ctDDH = new ChiTietDonDatHangBL();
        ChiTietDonDatHangDAL ChiTietDDH = new ChiTietDonDatHangDAL();
        SanPhamBL sp = new SanPhamBL();
        public frmQLyDonHang()
        {
            InitializeComponent();
        }
        
        protected void LoadDSDonHang()
        {
            dsDonHang = donHang.LayDSDonHang();
            for (int i = 0; i < dsDonHang.Count; i++)
            {
                ListViewItem lvi = new ListViewItem();

                lvi.Text = (i + 1).ToString();
                lvi.SubItems.Add(dsDonHang[i].MaDonDatHang.ToString());                
                lvi.SubItems.Add(dsDonHang[i].HoTenKH);
               // lvi.SubItems.Add(dsDonHang[i].SoDienThoai);
                lvi.SubItems.Add(dsDonHang[i].NgayDat.ToShortDateString());
                lvi.SubItems.Add(dsDonHang[i].TongTien.ToString());
                lvi.SubItems.Add(dsDonHang[i].TrangThaiGiaoHang.ToString());
                //them vo- ko hien len trong listview
                lvi.SubItems.Add(dsDonHang[i].SoDienThoai);
                lvi.SubItems.Add(dsDonHang[i].DiaChi.ToString());
                lvi.SubItems.Add(dsDonHang[i].Email.ToString());
                
                lvi.Tag = (object)dsDonHang[i].MaDonDatHang;

                lvDSDonHang.Items.Add(lvi);
                

            }

            
           
        }
        
        private void LayThongTin()
        {
            dh.HoTenKH = txtTenKH.Text.ToString();
            dh.DiaChi = txtDiaChi.Text;
            dh.Email = txtEmail.Text;
            dh.SoDienThoai = txtSoDT.Text;
            dh.TongTien = float.Parse(txtTongTien.Text.ToString());
            dh.NgayDat = Convert.ToDateTime(dtimeNgayDH.Value.ToShortDateString());
            dh.TrangThaiGiaoHang = int.Parse(cboTrangThaiGH.SelectedItem.ToString());
        }
        private void CapNhatThongTinChiTietDDH()
        {
            for (int i = 0; i < gridChiTietDDH.RowCount-1; ++i)
            {
                string maSP;
                ChiTietDDH.MaDonDatHang = int.Parse(txtMaDDH.Text.ToString());
                ChiTietDDH.MaSanPham = gridChiTietDDH[1, i].Value.ToString();
                maSP = gridChiTietDDH[3, i].Value.ToString();
                ChiTietDDH.SoLuong = int.Parse(gridChiTietDDH[4, i].Value.ToString());
                ChiTietDDH.ThanhTien = float.Parse(gridChiTietDDH[5, i].Value.ToString());
                ctDDH.CapNhatChiTietDonDatHang(ChiTietDDH,maSP);

 
            }
        }

        private void frmQLyDonHang_Load(object sender, EventArgs e)
        {
            loadCboTrangThaiGH();
            
            LoadDSDonHang();
            
        }

        private void lvDSDonHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.lvDSDonHang.Items.Count > 0)
            {
                ListViewItem lvi = this.lvDSDonHang.SelectedItems[0];

                //gan thuoc tinh tu lvi qua dh
                
                dh.MaDonDatHang = int.Parse(lvi.SubItems[1].Text);
                dh.HoTenKH = lvi.SubItems[2].Text;
                //dh.SoDienThoai = lvi.SubItems[3].Text;
                dh.NgayDat = Convert.ToDateTime(lvi.SubItems[3].Text);
                dh.TongTien = float.Parse(lvi.SubItems[4].Text);
                dh.TrangThaiGiaoHang = int.Parse(lvi.SubItems[5].Text);
                dh.SoDienThoai = lvi.SubItems[6].Text;
                dh.DiaChi = lvi.SubItems[7].Text;
                dh.Email = lvi.SubItems[8].Text;
               

                //gan thuoc tinh tu dh qua text
                
                txtMaDDH.Text = lvi.SubItems[1].Text;
                txtTenKH.Text = lvi.SubItems[2].Text; ;
               // txtSoDT.Text = lvi.SubItems[3].Text;
                dtimeNgayDH.Text = lvi.SubItems[3].Text;
                txtTongTien.Text = lvi.SubItems[4].Text;
                cboTrangThaiGH.Text = lvi.SubItems[5].Text;
                txtSoDT.Text = lvi.SubItems[6].Text;
                txtDiaChi.Text = lvi.SubItems[7].Text;
                txtEmail.Text = lvi.SubItems[8].Text;

                //load chi tiet don dat hang
                int maDDH = int.Parse(lvi.SubItems[1].Text.ToString());
                dsChiTietDDH = ctDDH.LayDSChiTietDonDatHang(maDDH);
                if (dsChiTietDDH.Count != 0)
                {
                    for (int i = 0; i < dsChiTietDDH.Count; i++)
                    {
                        this.gridChiTietDDH.Rows.Add();
                        this.gridChiTietDDH.Rows[i].Cells[0].Value = i + 1;
                        this.gridChiTietDDH.Rows[i].Cells[1].Value = dsChiTietDDH[i].MaSanPham;
                        this.gridChiTietDDH.Rows[i].Cells[2].Value = sp.LaySanPhamTheoMaSP(dsChiTietDDH[i].MaSanPham).TenSanPham;
                        this.gridChiTietDDH.Rows[i].Cells[3].Value = dsChiTietDDH[i].SoLuong;
                        this.gridChiTietDDH.Rows[i].Cells[4].Value = dsChiTietDDH[i].ThanhTien;
                    }
                }
                else
                {
                    this.gridChiTietDDH.Rows.Clear();
                }
                
            }
            lvDSDonHang.Items.Clear();
            LoadDSDonHang();
            
        }
        private void ClearALL()
        {
            txtMaDDH.Text = "";
            txtTenKH.Text = "";
            txtDiaChi.Text = "";
            txtEmail.Text = "";
            txtSoDT.Text = "";
            txtTongTien.Text = "";
            dtimeNgayDH.Text = DateTime.Now.ToShortDateString();
            gridChiTietDDH.Rows.Clear();
            
        }

        private void loadCboTrangThaiGH()
        {
            for (int i = -1; i <= 1; i++)
                cboTrangThaiGH.Items.Add(i.ToString());
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaDDH.Text == "")
            {
                MessageBox.Show("Bạn chưa chọn dữ liệu để xóa!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            
            
           
            if (dsDonHang.Count == 0)
            {
                MessageBox.Show("Không có dữ liệu để xóa!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            
            else
            {
                DialogResult Xoa = MessageBox.Show("Bạn có chắc muốn xóa đơn hàng này?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (Xoa == DialogResult.Yes)
                {
                    
                    ChiTietDonDatHangBL.XoaChiTietDonDatHangTheoDDH(dh.MaDonDatHang.ToString());
                    DonDatHangBL.XoaDonDatHang(dh.MaDonDatHang.ToString());
                    ClearALL();
                    lvDSDonHang.Items.Clear();
                    LoadDSDonHang();

                }

            }
            
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ClearALL();
        }
        

        private void btnNhapLai_Click(object sender, EventArgs e)
        {
           // dh.MaDonDatHang = int.Parse(txtMaDDH.Text.ToString());
            LayThongTin();
            if (donHang.ThemDonDatHang(dh))
            {
                
                MessageBox.Show("Da Them Thanh Cong", "Thong Bao", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            ClearALL();
            lvDSDonHang.Items.Clear();
            LoadDSDonHang();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMaDDH.Text == "")
                MessageBox.Show("Bạn chưa chọn đơn đặt hàng để cập nhật", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                dh.MaDonDatHang = int.Parse(txtMaDDH.Text.ToString());
                LayThongTin();
                CapNhatThongTinChiTietDDH();
                if (donHang.CapNhatDonHang(dh))
                {
                   
                    MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    lvDSDonHang.Items.Clear();
                    LoadDSDonHang();
                    ClearALL();
                }
            }
           
        }

        private void txtTongTien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(!Char.IsDigit(e.KeyChar)&&!Char.IsControl(e.KeyChar))
                e.Handled=true;	
        }

        private void txtSoDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;	
        }


        

        private void btnXemChiTiet_Click(object sender, EventArgs e)
        {
            int i;

            if (txtMaDDH.Text.ToString() == "")
            {
                MessageBox.Show("chua chon don dat hang", "thong bao", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            
            else
            {
                i = int.Parse(txtMaDDH.Text.ToString());
                frmChiTietDonDatHang frmChiTietDDH = new frmChiTietDonDatHang(i);
                frmChiTietDDH.Show();
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            ClearALL();
            lvDSDonHang.Items.Clear();
            DateTime ngay = Convert.ToDateTime(dtimeNgayTim.Value.ToShortDateString());
            dsDonHang = donHang.LayDSDonHangTheoThoiGian(ngay, ngay);
            for (int i = 0; i < dsDonHang.Count; i++)
            {
                ListViewItem lvi = new ListViewItem();

                lvi.Text = (i + 1).ToString();
                lvi.SubItems.Add(dsDonHang[i].MaDonDatHang.ToString());
                lvi.SubItems.Add(dsDonHang[i].HoTenKH);
                // lvi.SubItems.Add(dsDonHang[i].SoDienThoai);
                lvi.SubItems.Add(dsDonHang[i].NgayDat.ToShortDateString());
                lvi.SubItems.Add(dsDonHang[i].TongTien.ToString());
                lvi.SubItems.Add(dsDonHang[i].TrangThaiGiaoHang.ToString());
                //them vo- ko hien len trong listview
                lvi.SubItems.Add(dsDonHang[i].SoDienThoai);
                lvi.SubItems.Add(dsDonHang[i].DiaChi.ToString());
                lvi.SubItems.Add(dsDonHang[i].Email.ToString());

                lvi.Tag = (object)dsDonHang[i].MaDonDatHang;

                lvDSDonHang.Items.Add(lvi);


            }

        }

        private void btnLoadLai_Click(object sender, EventArgs e)
        {
            LoadDSDonHang();
            
        }

        




        
    }
}
